package laravel.mvc.view.blade;

import entity_model.Entity;
import laravel.Laravel;
import laravel.mvc.view.View;
import utils.files.File;
import utils.titles.Title;

import static laravel.mvc.view.route.Route.Method.GET;
import static laravel.mvc.view.route.Route.Method.POST;

/**
 * Created by thormentor on 28-04-2014.
 */
public class Blade extends File {

    protected final String lowerUnderscoreEntityName;

    public Blade(Laravel laravel, Entity entity, Title title) {
        this(
                laravel,
                entity,
                entity.getTitle().toSnakeCase(),
                title
        );
    }

    protected final Section content = new Section("content");

    private Blade(Laravel laravel, Entity entity, String lowerUnderscoreEntityName, Title title) {
        super(View.FOLDER.create(lowerUnderscoreEntityName), title.toLowerCamel(), "blade.php");
        this.lowerUnderscoreEntityName = lowerUnderscoreEntityName;

        add(new Extends("layout.main"));
        add(content);

        final String entityNameUpperCamel = entity.getTitle().toUpperCamel();
        laravel.getRoutes().createRoute(
                GET,
                "/" + lowerUnderscoreEntityName + "/" + title.toLowerCamel(),
                lowerUnderscoreEntityName + "-" + title.toLowerCamel(),
                entityNameUpperCamel + "Controller@get" + title.toUpperCamel()
        );

        laravel.getRoutes().createRoute(
                POST,
                "/" + lowerUnderscoreEntityName + "/" + title.toLowerCamel(),
                lowerUnderscoreEntityName + "-" + title.toLowerCamel() + "-post",
                entityNameUpperCamel + "Controller@post" + title.toUpperCamel()
        );

        laravel.getLeftMenu().createLink(title.toUpperCamel() + entityNameUpperCamel, lowerUnderscoreEntityName + "-" + title.toLowerCamel());
    }

}
